define(function (require,exports,module) {
    var angular = require('angular');

    require('business_modules/material/modules/voice/voiceService');
    require('business_modules/material/modules/uploaderService');

    var material = angular.module('material.voiceController',[
        'uploader'
    ]);

    material.controller('voice',[
        '$scope',
        '$rootScope',
        'uploaderService',
        'voiceService',
        'limit',
        function ($scope,$rootScope,uploaderService,voiceService,limit) {

            $scope.voiceEdit =  {
                title: "编辑名称",
                text: function (index) {
                    return $scope.voiceList[index].name;
                },
                validate: function (text) {
                    return true;
                },
                ok: function (name,index) {
                    $scope.updateVoiceName(name,$scope.voiceList[index].id,index);
                }
            };

            $scope.voiceDelete = {
                title: "确定删除此素材？",
                followType: "text",
                ok: function (index) {
                    $scope.deleteVoice($scope.voiceList[index].id,index);
                }
            };

            $scope.pagination = {
                pages: 0,
                current: 1,
                change: function (page) {
                    $scope.searchVoiceList(page);
                }
            };

            $scope.searchVoiceList = function (page) {
                voiceService.getVoiceList({
                    search: {
                        limit: limit,
                        offset: limit * (page - 1)
                    },
                    success: function (res) {
                        $scope.voiceList = res.data.data;
                        $scope.pagination.pages = Math.ceil(res.data.total / limit);
                    },
                    fail: function (res) {
                        $rootScope.$emit('httpRequestFail','获取语音列表失败');
                    }
                });
            };

            $scope.deleteVoice = function (voiceId) {
                voiceService.deleteVoice({
                    search: {
                        id: voiceId
                    },
                    success: function (res) {
                        $scope.searchVoiceList(1);
                    },
                    fail: function (res) {
                        $rootScope.$emit('httpRequestFail','删除音频失败');
                    }
                });
            };

            $scope.updateVoiceName = function (voiceName,voiceId,index) {
                voiceService.updateVoiceName({
                    search: {
                        id: voiceId
                    },
                    data: {
                        type: 'voice',
                        data: {
                            name: voiceName
                        }
                    },
                    success: function (res) {
                        // $scope.searchVoiceList($scope.pagination.current);
                        $scope.voiceList[index].name = voiceName;
                    },
                    fail: function (res) {
                        $rootScope.$emit('httpRequestFail','编辑名称失败');
                    }
                });
            };

            $scope.uploader = uploaderService.createFileUploader({
                api: "material/upload",
                data: [{type:'voice'}],
                filters:[
                    {
                        name: 'voiceFilter',
                        fn: function (item) {
                            return /\.(mp3|wma|wav|amr)$/.test(item.name);
                        }
                    },{
                        name: 'sizeLimit',
                        fn: function (item) {
                            return item.size <= 5 * 1024 * 1024;
                        }
                    }
                ],
                success: function (res) {
                    $scope.searchVoiceList($scope.pagination.current);
                    $rootScope.$emit('httpRequestEnd');
                    $rootScope.$emit('httpRequestSuccess','上传成功');
                },
                error: function (filter) {
                    if (filter === "voiceFilter") {
                        alert('请上传正确的音频文件');
                    } else if (filter === "sizeLimit"){
                        alert('请选择5M以内的音频文件');
                    }
                },
                onBeforeUploadItem: function () {
                    $rootScope.$emit('httpRequestStart','正在上传');
                },
                fail: function (res) {
                    $rootScope.$emit('httpRequestFail','上传失败');
                }
            });

            $scope.searchVoiceList(1);
        }
    ]);
});
